package com.ruoyi.backend.controller;

import com.ruoyi.backend.pojo.Field;
import com.ruoyi.backend.service.FieldService;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.DataSourceType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

@Api(tags = "外业调查数据管理相关接口", value = "MySQL数据库主导执行(所有接口均已通过测试)")
@RequestMapping("/backend/field")
@RestController
public class FieldController {

    @Autowired
    private FieldService fieldService;

    @DataSource(DataSourceType.MASTER)
    @ApiOperation(value = "上传外业调查表数据接口")
    @PostMapping("/upload")
    public R<Field> upload(@RequestBody @Validated(Field.Upload.class) Field field){

        Field uploadResult = fieldService.upload(field);
        return R.ok(uploadResult ,"外业调查表数据上传成功");
    }

    @DataSource(DataSourceType.MASTER)
    @ApiOperation(value = "删除外业调查表数据接口")
    @DeleteMapping("/delete")
    public R<String> delete(@RequestParam("id") @NotNull(message = "外业调查ID不能为空") Integer investigateId){

        fieldService.delete(investigateId);
        return R.ok("外业调查表数据删除成功");
    }

    @DataSource(DataSourceType.MASTER)
    @ApiOperation(value = "更新外业调查表数据接口")
    @PutMapping("/update")
    public R<String> update(@RequestBody @Validated(Field.Update.class) Field field){

        fieldService.update(field);
        return R.ok("外业调查数据更新成功");
    }

    @DataSource(DataSourceType.MASTER)
    @ApiOperation(value = "查询外业调查表数据接口")
    @GetMapping("/search")
    public R<List<Field>> search(@RequestParam(name = "mineId", required = false) String mineId,
                         @RequestParam(name = "investigateType", required = false) String investigateType,
                         @RequestParam(name = "investigateName", required = false) String investigateName,
                         @RequestParam(name = "investigateUnit", required = false) String investigateUnit
    ){

        List<Field> fieldList = fieldService.search(mineId, investigateType, investigateName, investigateUnit);
        return R.ok(fieldList,"查询外业调查数据成功");
    }

    @DataSource(DataSourceType.MASTER)
    @ApiOperation(value = "查询所有外业调查表数据接口")
    @GetMapping("/all")
    public R<List<Field>> all(){

        List<Field> fieldList = fieldService.searchAll();
        return R.ok(fieldList,"查询所有外业调查数据成功");
    }
}
